<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ncduc
 * Date: 7/3/13
 * Time: 6:31 PM
 * To change this template use File | Settings | File Templates.
 */
include_once("../libs/setup.php");
include_once("../libs/helper.php");
session_start();

if (!check_user_session()) {
    header("Location: ../signin.php");
    return;
}
assign_home_header($smarty);
$smarty->assign('wwwUrl','../');
function load_research_projects(mysqli $mysqli, $userId, Smarty $smarty)
{
    $query = "SELECT * FROM de_tai_nghien_cuu WHERE MaUser=$userId";

    $result = $mysqli->query($query);
    $arr_research_projects = array();
    if ($result) {

        while ($row = $result->fetch_assoc()) {

            $arr_research_projects[] = array("Ma" => $row["Ma"],
                "TenDeTai_vi" => $row["TenDeTai_vi"],
                "TenDeTai_en" => $row["TenDeTai_en"],
                "TenToChucTaiTro_vi" => $row["TenToChucTaiTro_vi"],
                "TenToChucTaiTro_en" => $row["TenToChucTaiTro_en"],
                "DiaChi" => $row["DiaChi"],
                "DiaChiWebsite" => $row["DiaChiWebsite"],
                "VaiTro_vi" => $row["VaiTro_vi"],
                "VaiTro_en" => $row["VaiTro_en"],
                "ThoiGianBatDau" => $row["ThoiGianBatDau"],
                "ThoiGianKetThuc" => $row["ThoiGianKetThuc"],
                "TongKinhPhi" => $row["TongKinhPhi"],
                "DonViTienTe" => $row["DonViTienTe"],
                "ThongTinThem" => $row["ThongTinThem"]);

        }

    }
    if ($mysqli->error) {
        system_failed($mysqli->error, $smarty);
        $mysqli->close();
        return false;
    }

    $smarty->assign('projects', $arr_research_projects);
    return true;
}

function load_research_results(mysqli $mysqli, $userId, Smarty $smarty)
{
    $query = "SELECT * FROM ket_qua_nghien_cuu WHERE MaUser=$userId";
    $result = $mysqli->query($query);
    if ($result) {
        $arr_research_results = array();
        while ($row = $result->fetch_assoc()) {
            $arr_research_results[] = array("Ma" => $row["Ma"],
                "TenSanPham" => $row["TenSanPham"],
                "TenTacGia" => $row["TenTacGia"],
                "TenTapChi" => $row["TenTapChi"],
                "MaLoaiKQNghienCuu" => $row["MaLoaiKQNghienCuu"],
                "ISSN" => $row["ISSN"],
                "MinhChung" => $row["MinhChung"],
                "GhiChu" => $row["GhiChu"]
            );
        }
    }
    if ($mysqli->error) {
        system_failed($mysqli->error, $smarty);
        $mysqli->close();
        return false;
    }
    $smarty->assign('results', $arr_research_results);
    return true;
}

function get_research_direction(mysqli $mysqli, $userId, Smarty $smarty)
{
    $query = "SELECT * FROM hoat_dong_nghien_cuu WHERE MaUser={$userId}";
    $result = $mysqli->query($query);
    if ($result) {
        while ($row = $result->fetch_assoc()) {
            $id = $row['Ma'];
            $research_oriented_vi = $row['HuongNghienCuu_vi'];
            $research_oriented_en = $row['HuongNghienCuu_en'];
            $idea_uploaded = $row['YTuongUpload'];
            $smarty->assign('id', $id);
            $smarty->assign('research_oriented_vi', $research_oriented_vi);
            $smarty->assign('research_oriented_en', $research_oriented_en);
            $smarty->assign('idea_uploaded',$idea_uploaded);

        }
    }
    if ($mysqli->error) {
        system_failed($mysqli->error, $smarty);
        $mysqli->close();
        return false;
    }

    return true;
}

function save_research_activities($userID,mysqli $mysqli, Smarty $smarty)
{
    $research_oriented_vi = addslashes($_REQUEST['research_oriented_vi']);
    $research_oriented_en = addslashes($_REQUEST['research_oriented_en']);
    $sql = "update hoat_dong_nghien_cuu set HuongNghienCuu_vi = '{$research_oriented_vi}',HuongNghienCuu_en = '{$research_oriented_en}' where `MaUser`=$userID";
    $result = $mysqli->query($sql);
    if ($mysqli->error) {
          system_failed($mysqli->error . "sql: ". $sql, $smarty);
          $mysqli->close();
          return false;
      }
    return true;
}
function save_upload_idea($userID, mysqli $mysqli, Smarty $smarty)
{
    $upload_result = upload_file('idea_upload', $userID);
    if (strlen($upload_result) > 0) //upload file error
    {
        $message = $upload_result;
        $smarty->assign('is_display', "block");
        $smarty->assign('message', $message);
    } else {
        $filename = $_FILES['idea_upload']['name'];
        $sql = "UPDATE hoat_dong_nghien_cuu SET `YTuongUpload` = '{$filename}' WHERE Ma = {$userID}";
        $result = $mysqli->query($sql);
        if ($mysqli->error) {
            system_failed(NULL, $smarty);
            $mysqli->close();
            return false;
        }
    }
    return true;
}

$mysqli = connect_database($smarty);
$userId = $_SESSION['ma_user'];
if (!load_research_projects($mysqli, $userId, $smarty))
    return;
if (!load_research_results($mysqli, $userId, $smarty))
    return;
if (empty($_REQUEST['save']) && empty($_REQUEST['next'])) {

    if (!get_research_direction($mysqli, $userId, $smarty))
        return;


    $smarty->display('profile/4.tpl');
} else {
    if (isset($_REQUEST['save'])) {

        $id = $_REQUEST['id'];
        if (!save_research_activities($userId,$mysqli, $smarty))
            return;
        if($_FILES['idea_upload']['size']>0)
        {
            if(!save_upload_idea($userId, $mysqli, $smarty))
                  return;
        }
        $smarty->assign($_REQUEST);
        $smarty->display('profile/4.tpl');
    } else //Next button
    {
        header("Location: 5.php");
    }
}
$mysqli->close();